package dj.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dj.dao.AppCategoryMapper;
import dj.dao.AppInfoMapper;
import dj.dao.AppVersionMapper;
import dj.dao.DataDictionaryMapper;
import dj.entity.AppCategory;
import dj.entity.AppInfo;
import dj.entity.AppVersion;
import dj.entity.DataDictionary;
import dj.service.AppInfoService;


@Service
public class AppInfoServiceImpl implements AppInfoService{
	@Resource
	private AppInfoMapper appInfoMapper;
	
	@Resource
	private DataDictionaryMapper dataDictionaryMapper;
	
	@Resource
	private AppCategoryMapper appCategoryMapper;

	@Resource
	private AppVersionMapper appVersionMapper;

	@Override
	public List<AppInfo> selectPage(Map map) {
		// TODO Auto-generated method stub
		return appInfoMapper.selectPage(map);
	}

	@Override
	public Integer selectAll(Map map) {
		// TODO Auto-generated method stub
		return appInfoMapper.selectAll(map);
	}

	@Override
	public List<DataDictionary> getDataDictionaryList() {
		// TODO Auto-generated method stub
		return dataDictionaryMapper.getDataDictionaryList();
	}

	@Override
	public List<AppCategory> getCategoryList(Integer parentId) {
		// TODO Auto-generated method stub
		return appCategoryMapper.getCategoryList(parentId);
	}

	@Override
	public AppInfo getAppInfo(Integer id) {
		// TODO Auto-generated method stub
		return appInfoMapper.getAppInfo(id);
	}

	@Override
	public AppVersion getAppVersion(Integer id) {
		// TODO Auto-generated method stub
		return appVersionMapper.getAppVersion(id);
	}

	@Transactional
	@Override
	public boolean update(Map map) {
		if(appInfoMapper.update(map)>0){
			return true;
		}
		return false;
	}

	@Override
	public List<DataDictionary> getDataDictionaryStatus() {
		// TODO Auto-generated method stub
		return dataDictionaryMapper.getDataDictionaryStatus();
	}

	@Override
	public AppInfo getAppInfoIdName(Integer id, String softwareName,
			String APKName) {
		Map<String,Object> map=new HashMap<String, Object>();
		if(id!=null){
			map.put("id",id);
		}
		if(softwareName!=null&&!softwareName.equals("")){
			map.put("softwareName", softwareName);
		}
		if(APKName!=null&&!APKName.equals("")){
			map.put("APKName",APKName);
		}
		return appInfoMapper.getAppInfoIdName(map);
		
	}

	@Transactional
	@Override
	public boolean insert(AppInfo record) {
		if(appInfoMapper.insert(record)>0){
			return true;
		}
		return false;
	}

	@Transactional
	@Override
	public boolean updateAppInfo(AppInfo record) {
		if(appInfoMapper.updateByPrimaryKey(record)>0){
			return true;
		}
		return false;
	}

	@Transactional
	@Override
	public boolean updatelogo(Integer id) {
		if(appInfoMapper.updatelogo(id)>0){
			return true;
		}
		return false;
	}

	@Override
	public List<AppVersion> AppVersionApp(Integer aid) {
		// TODO Auto-generated method stub
		return appVersionMapper.AppVersionApp(aid);
	}

	@Transactional
	@Override
	public boolean insertAppVersion(AppVersion appVersion) {
		if(appVersionMapper.insert(appVersion)>0){
			return true;
		}
		return false;
	}

	@Transactional
	@Override
	public boolean updateAppVersionId(Map<String, Object> map) {
		if(appInfoMapper.updateAppVersion(map)>0){
			return true;
		}
		return false;
	}

	@Transactional
	@Override
	public boolean updateApk(Integer id) {
		if(appVersionMapper.updateApk(id)>0){
			return true;
		}
		return false;
	}

	@Transactional
	@Override
	public boolean updateAppVersion(AppVersion appVersion) {
		if(appVersionMapper.updateAppVersion(appVersion)>0){
			return true;
		}
		return false;
	}

	@Transactional
	@Override
	public boolean delAppInfo(Integer id) {
		if(appVersionMapper.delAppId(id)>0&&appInfoMapper.deleteByPrimaryKey(id)>0){
			return true;
		}
		return false;
	}

	@Override
	public boolean updateAppStatus(Integer id, Long devId) {
		AppInfo appInfo=appInfoMapper.getAppInfo(id);
		if(appInfo!=null){
			if(appInfo.getStatus()==2||appInfo.getStatus()==5){
				appInfo.setStatus(Long.valueOf("4"));
				appInfo.setOnSaleDate(new Date());
			}else if(appInfo.getStatus()==4){
				appInfo.setStatus(Long.valueOf("5"));
				appInfo.setOffSaleDate(new Date());
			}
		}
		appInfo.setModifyBy(devId);
		if(appInfoMapper.updateAppStatus(appInfo)>0){
			return true;
		}
		return false;
	}
	
	
	
	
}
